<%@ page import="com.apexedu.identity.security.CasLoginUtil" %>
<%@ page import="com.apexedu.portal.constant.Constants" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title><%=Constants.ProjectInfo.PROJECT_NAME%>
    </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<div id="main">
    <c:choose>
        <c:when test="${type=='find'}">
            <jsp:include page="../shgh/headNologin.jsp"/>
        </c:when>
        <c:otherwise>
            <jsp:include page="<%=Constants.ProjectInfo.HEAD_VIEW%>"/>
        </c:otherwise>
    </c:choose>
    <script>
        function calcPasswordSecurityLevel(password) {
            var result = 0;
            var reg = null;
            if (null != password && "" != password) {
                reg = /([0-9])+/g;
                if (password.replace(reg, "").length < password.length) {
                    result++;
                }
                reg = /([a-z])+/g;
                if (password.replace(reg, "").length < password.length) {
                    result++;
                }
                reg = /([A-Z])+/g;
                if (password.replace(reg, "").length < password.length) {
                    result++;
                }
                reg = /([@#$&*_])+/g;
                if (password.replace(reg, "").length < password.length) {
                    result++;
                }
                reg = /.{6,}/g;
                if (!password.match(reg)) {
                    result = 1;
                }
            }
            return result;
        }

        function doreset() {
            $(":password").val("");
        }
        function doSubmit() {
            var oldpwd = $("#userPwdOld").val().replace(/(^\s*)|(\s*$)/g, "");
            var userPwd = $("#userPwd").val().replace(/(^\s*)|(\s*$)/g, "");
            var dlmmnew2 = $("#dlmmnew2").val().replace(/(^\s*)|(\s*$)/g, "");
            if (oldpwd == "") {
                alert("原密码不能为空");
                return false;
            }
            if (userPwd == "") {
                alert("新密码不能为空！");
                return false;
            }
            if (userPwd != dlmmnew2) {
                alert("两次输入的密码不一致");
                return false;
            }

            if (calcPasswordSecurityLevel(userPwd) < 2) {
                alert("新密码至少需要包含数字、小写字母、大写字母、特殊符号中的两种类型，并且长度大于等于6个字符！");
                return false;
            }

            //首先检测原密码是否正确
            $.post('${basePath}/s/uc/us/cs', {pwd: encodeURIComponent(oldpwd)}, function (data) {
                if (data == 'true') {
                    //
                    $.post('${basePath}/s/uc/us/ms', {pwd: encodeURIComponent(userPwd)}, function (data) {
                        if (data == 'true') {
                            alert("修改密码成功！");
                            $.get("${basePath}/identity/ajaxlogin.action?act=logout", {q: (new Date()).getTime()}, function (data) {
                                data = eval("(" + data + ")");
                                if (data.msg == 'success') {
                                    <%
                                if( CasLoginUtil.isSsoEnabled() ){
                                %>
                                    <%--window.location.href = '<%=CasLoginUtil.getCasServerLogoutUrl()%>';--%>
                                    window.location.href = '/';
                                    <%
                                    }else{
                                    %>
                                    window.location.href = '<%=request.getContextPath() %>?t=' + Math.round(new Date / 1000);
                                    <%
                                    }
                                    %>
                                } else {
                                    alert(data.msg);
                                }
                            });
                        } else {
                            alert("修改密码失败！");
                        }
                    });
                } else {
                    alert("原密码输入有误,请重新输入");
                }
            });
        }

        function doNext() {
            var userPwd = $("#userPwd").val().replace(/(^\s*)|(\s*$)/g, "");
            var dlmmnew2 = $("#dlmmnew2").val().replace(/(^\s*)|(\s*$)/g, "");
            var userid = $("#userid").val();
            if (userPwd == "") {
                alert("新密码不能为空！");
                return false;
            }
            if (userPwd != dlmmnew2) {
                alert("两次输入的密码不一致");
                return false;
            }
            $.post('${basePath}/s/us/ms', {pwd: encodeURIComponent(userPwd), userid: userid}, function (data) {
                if (data == 'true') {
                    alert("修改密码成功！");
                    window.location.href = ${basePath}+"/s/uc/shgh/i";
                } else {
                    alert("修改密码失败！");
                }
            });
        }
    </script>
    <div id="content">
        <div id="contentSub" style="height: 300px;">
            <div>
                <fieldset style="border: 1px solid #ccc;">
                    <c:choose>
                        <c:when test="${type=='find'}">
                            <legend style="font-size: 18px;text-align: left;">3、修改密码</legend>
                        </c:when>
                        <c:otherwise>
                            <legend style="font-size: 18px;text-align: left;">密码修改</legend>
                        </c:otherwise>
                    </c:choose>
                    <table class="tableStyle" style="background-color: transparent;">
                        <c:choose>
                            <c:when test="${type=='find'}">
                                <input type="hidden" name="userid" id="userid" value="${userid}"/>
                            </c:when>
                            <c:otherwise>
                                <tr>
                                    <td width="30%" height="35" align="right">原密码：</td>
                                    <td width="70%" align="left">
                                        <input type="password" id="userPwdOld" tabindex="2" maxlength="50" size="15"
                                               name="tsysUser.username">
                                        <font color="#FF0000">*</font> <font color="#666666">输入您正在使用的密码，用于确认您的身份！</font>
                                    </td>
                                </tr>
                            </c:otherwise>
                        </c:choose>
                        <tr>
                            <td width="30%" height="35" align="right">新密码：</td>
                            <td width="70%" align="left">
                                <input type="password" id="userPwd" tabindex="2" maxlength="50" size="15"
                                       name="tsysUser.userpwd" class="textinput">
                                <font color="#FF0000">*</font> <font color="#666666">输入新密码！</font>
                            </td>
                        </tr>
                        <tr>
                            <td width="30%" height="35" align="right">确认新密码：</td>
                            <td width="70%" align="left">
                                <input type="password" maxlength="50" size="15" id="dlmmnew2" name="dlmmnew2"
                                       class="textinput">
                                <font color="#FF0000">*</font> <font color="#666666">请再输入一次上面写的新密码！</font>
                            </td>
                        </tr>

                        <tr>
                            <td width="80%" height="50" align="left" colspan="4" style="text-align: center;">
                                <c:choose>
                                    <c:when test="${type=='find'}">
                                        <input type="button" onclick="window.location.href = '${basePath}/s/mh/fp/qe'"
                                               value="上一步" class="button">
                                        <input type="button" onclick="doNext()" value="提交" class="button">
                                    </c:when>
                                    <c:otherwise>
                                        <input type="button" onclick="doSubmit()" value="提交" class="button">
                                        <input type="button" onclick="doreset()" value="清除" class="button">
                                    </c:otherwise>
                                </c:choose>
                            </td>
                            <td width="20%" style="text-align: center; "></td>
                        </tr>
                    </table>
                </fieldset>
            </div>
        </div>
        <div id="contentBg"></div>
    </div>
    <jsp:include page="<%=Constants.ProjectInfo.FOOT_VIEW%>"/>
</div>
</body>
</html>
